package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.domain.system.SysDict;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.service.system.SysDictService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * @AuThor：zengxing
 * @DATE:2020/12/24 10:27  250
 */
@Controller
@RequestMapping("/cargo/factory")
public class FactoryController extends BaseController {

    @Reference
    private FactoryService factoryService;

    @Autowired
    private SysDictService sysDictService;
    /**
     * 分页查询
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo pageInfo = factoryService.findByPage(new FactoryExample(),pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/factory/factory-list";
    }

    /**
     * 进入修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String type, Model model) {
        // 根据id查询
        Factory factory = factoryService.findById(id);
        // 保存
        List<SysDict> SysDictList = sysDictService.findByType(type);
        request.setAttribute("SysDictList",SysDictList);
        model.addAttribute("factory", factory);
        return "cargo/factory/factory-update";
    }

    /**
     * 添加或修改
     */
    @RequestMapping("/edit")
    public String edit(Factory factory) {
        // 设置企业信息（从登陆用户中获取）


        // 判断：根据id是否为空判断执行添加还是修改
        if (StringUtils.isEmpty(factory.getId())) {
            // 添加
            factory.setCreateBy(getLoginUser().getUserName());
            factory.setCreateDept(getLoginUser().getDeptName());
            factory.setCreateTime(new Date());
            factoryService.save(factory);
        } else {
            // 修改
            factoryService.update(factory);
        }

        return "redirect:/cargo/factory/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        Factory factory = new Factory();
        factory.setId(id);
        factory.setState(0);
        factoryService.update(factory);
        return "redirect:/cargo/factory/list";
    }


    /**
     * 进入添加页面
     */
    @RequestMapping("/toAdd")
    public String toAdd(String type) {
        List<SysDict> SysDictList = sysDictService.findByType(type);
        request.setAttribute("SysDictList",SysDictList);
        return "cargo/factory/factory-add";
    }
}
